package com.itheima.behavior.controller.v1;

import com.itheima.behavior.service.BehaviorTrackService;
import com.itheima.model.behavior.dtos.BehaviorDto;
import com.itheima.model.common.dtos.PageResponseResult;
import com.itheima.model.common.dtos.ResponseResult;
import com.itheima.utils.thread.MpThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 行为追踪控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/behavior")
@Api(tags = "行为数据追踪接口")
public class BehaviorTrackController {

    @Autowired
    private BehaviorTrackService behaviorTrackService;

    /**
     * 单条行为上报
     */
    @PostMapping("/track")
    @ApiOperation("单条行为上报")
    public ResponseResult trackBehavior(@RequestBody BehaviorDto dto) {
        return behaviorTrackService.trackBehavior(dto);
    }

    /**
     * 批量行为上报（推荐使用）
     */
    @PostMapping("/track/batch")
    @ApiOperation("批量行为上报")
    public ResponseResult trackBehaviorBatch(@RequestBody List<BehaviorDto> dtoList) {
        return behaviorTrackService.trackBehaviorBatch(dtoList);
    }

    /**
     * 开始阅读（开始计时）
     */
    @PostMapping("/reading/start/{bookId}")
    @ApiOperation("开始阅读")
    public ResponseResult startReading(
            @ApiParam("图书ID") @PathVariable Integer bookId) {
        return behaviorTrackService.startReading(bookId);
    }

    /**
     * 结束阅读（结束计时）
     */
    @PostMapping("/reading/end/{bookId}")
    @ApiOperation("结束阅读")
    public ResponseResult endReading(
            @ApiParam("图书ID") @PathVariable Integer bookId,
            @ApiParam("阅读时长（秒）") @RequestParam Integer duration) {
        return behaviorTrackService.endReading(bookId, duration);
    }

    /**
     * 获取用户行为统计
     */
    @GetMapping("/stats/{userId}")
    @ApiOperation("获取用户行为统计")
    public ResponseResult getUserBehaviorStats(
            @ApiParam("用户ID") @PathVariable Integer userId) {
        return behaviorTrackService.getUserBehaviorStats(userId);
    }

    /**
     * 获取浏览记录列表
     */
    @GetMapping("/history")
    @ApiOperation("获取浏览记录列表")
    public PageResponseResult getBrowsingHistory(
            @ApiParam("时间筛选: all/today/week/month") @RequestParam(defaultValue = "all") String timeFilter,
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer size) {
        Integer userId = MpThreadLocalUtil.getUser().getId();
        return behaviorTrackService.getBrowsingHistory(userId, timeFilter, page, size);
    }

    /**
     * 删除单条浏览记录
     */
    @DeleteMapping("/history/{recordId}")
    @ApiOperation("删除单条浏览记录")
    public ResponseResult deleteBrowsingRecord(
            @ApiParam("记录ID") @PathVariable String recordId) {
        Integer userId = MpThreadLocalUtil.getUser().getId();
        return behaviorTrackService.deleteBrowsingRecord(userId, recordId);
    }

    /**
     * 清空所有浏览记录
     */
    @DeleteMapping("/history/clear")
    @ApiOperation("清空所有浏览记录")
    public ResponseResult clearAllBrowsingHistory() {
        Integer userId = MpThreadLocalUtil.getUser().getId();
        return behaviorTrackService.clearAllBrowsingHistory(userId);
    }
}

